classdef CalcPrimes < handle
    %CalcPrimes Finds all primes up to Nmax
    %   Example Usage: 
    % FP=CalcPrimes()
    % FP.Nmax=100;
    % FP.FindPrimes()
    %
    
    properties
        Nmax=10;        %Number of prime numbers to find
        FoundPrimes;    %The array of found prime numbers
    end
    
    methods
        function FindPrimes(obj)
            
            %Counter for number we have found
            Nprime=0;
            
            %Our current integer to be tested. We start with '2'
            Ntest=2;
            
            %create an empty array of the size we want. 
            primes=zeros(obj.Nmax,1);
            
            while Nprime<obj.Nmax %don't stop until we find Nmax primes
                
                %use a function to replace code.  This is easier to read and
                %understand. Note the syntax for calling a class method!
                [ isP ] = obj.CheckPrime(Ntest);
                
                if isP==1 %then it is prime!
                    Nprime=Nprime+1;    %we found another, so increment our counter
                    primes(Nprime)=Ntest; %put our found prime into the array
                end
                
                Ntest=Ntest+1; %increment the integer that we will test
            end
            
            obj.FoundPrimes=primes;  %Assign the result to our class property 'FoundPrimes'
            
        end
        
        function isP=CheckPrime(obj,Ntest)
            
            %Our code for checking to see if any integer divides into Ntest
            %without remainder. 
            notPrime=0;
            for ii=2:Ntest-1
                if Ntest/ii==round(Ntest/ii)
                    notPrime=1;
                end
            end
            isP= (~notPrime);
            
        end
        
    end
    
end

